package com.example.common_base.net

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import java.util.concurrent.TimeUnit


object SystemApi {
    //超时时间
    val DEFAULT_TIMEOUT = 10;

    private lateinit var retrofit: Retrofit

    fun provideRetrofit(): Retrofit {
        if (!::retrofit.isInitialized) {
            val client = OkHttpClient.Builder()
                .connectTimeout(DEFAULT_TIMEOUT.toLong(), TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT.toLong(), TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT.toLong(), TimeUnit.SECONDS)
                .addInterceptor(InterceptorUtil.headerParamsInterceptor())
                .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
                .build()

            retrofit = Retrofit.Builder()
                .baseUrl(NetParams.SERVER_HOST) //服务器项目地址
                .addConverterFactory(HttpResultConverter.create())//数据转换器，重写了下面有代码
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())

                .client(client)
                .build()
        }

        return retrofit
    }


    fun <T> provideService(t: Class<T>): T {
        return provideRetrofit().create(t)
    }

}